package com.zhkj.txg.base

import com.google.gson.Gson
import com.zhkj.lib.base.BaseApplication
import com.zhkj.lib.config.BUGLY_APP_ID
import com.zhkj.txg.BuildConfig
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.shuyu.gsyvideoplayer.cache.CacheFactory
import com.shuyu.gsyvideoplayer.player.PlayerFactory
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
import com.tencent.bugly.crashreport.CrashReport
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.SPUtil
import com.zhkj.txg.module.home.entity.ShopInfo
import tv.danmaku.ijk.media.exo2.Exo2PlayerManager
import tv.danmaku.ijk.media.exo2.ExoPlayerCacheManager
import java.lang.Exception


class CustomApplication : BaseApplication() {
    override fun prepareOnMainThread() {
        // 配饰GSY视频播放
        PlayerFactory.setPlayManager(Exo2PlayerManager::class.java)
        CacheFactory.setCacheManager(ExoPlayerCacheManager::class.java)
        GSYVideoType.setRenderType(GSYVideoType.TEXTURE)
        // 配置bugly
        if (!BuildConfig.DEBUG) CrashReport.initCrashReport(applicationContext, BUGLY_APP_ID, false)
        // 设置全局下拉刷新UI
        SmartRefreshLayout.setDefaultRefreshInitializer { _, layout ->
            layout.setEnableRefresh(true).setEnableOverScrollBounce(false)
                .setEnableLoadMore(false).setEnableOverScrollDrag(true)
                .setEnableHeaderTranslationContent(true).setHeaderHeight(60F)
        }
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, _ ->
            ClassicsHeader(context).setTextSizeTime(11F).setEnableLastTime(true)
                .setDrawableArrowSize(14F).setTextSizeTitle(13F)
        }
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, _ ->
            ClassicsFooter(context).setTextSizeTitle(11F).setDrawableArrowSize(14F).setTextSizeTitle(13F)
        }
    }

    override fun prepareOnAllThread() = Unit

    companion object {

        fun getShopInfo(): ShopInfo? {
            val shopInfo1 = SPUtil.getInstance().getString(AccountManager.SHOP_INFO)
            return try {
                val gson = Gson()
                gson.fromJson<ShopInfo>(shopInfo1, ShopInfo::class.java)
            } catch (e: Exception) {
                null
            }
        }

        fun getShopId(): Int {
            val shopInfo = getShopInfo()
                ?: return 0
            val shopId = shopInfo.id
                ?: return 0
            return shopId.toInt()
        }

        fun getShopMobile():String{
            val shopInfo = getShopInfo()
                ?: return ""
            val shopMobile = shopInfo.mobile
                ?: return ""
            return shopMobile
        }
    }
}